package org.eclipse.pde.internal.ui.editor.product;

import java.util.HashMap;
import org.eclipse.jface.resource.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.pde.core.plugin.*;
import org.eclipse.pde.internal.core.iproduct.ICustomizationInfo;
import org.eclipse.pde.internal.core.iproduct.ITransformationsManager;
import org.eclipse.pde.internal.ui.editor.plugin.ExtensionLabelProvider;
import org.eclipse.pde.internal.ui.editor.plugin.ExtensionsSection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.BidiUtil;
import org.eclipse.ui.forms.FormColors;
import org.eclipse.ui.forms.widgets.FormToolkit;

public class ExtensionCustomizationLabelProvider extends ExtensionLabelProvider implements IColorProvider, IFontProvider {

	private static final String DISABLED = "pde.product.DISABLED"; //$NON-NLS-1$
	private static final String TRANSFORMED = "pde.product.TRANSFORMED"; //$NON-NLS-1$
	private static final String NAMED_PROVIDER_ID = "namedLabelProvider"; //$NON-NLS-1$

	private ICustomizationInfo fCustomizationInfo;
	private ResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources());
	private FormToolkit fToolkit;
	private HashMap fLabelProviders;
	//managed by resource manager
	private Color fDisabledColor;
	private Font fDisabledFont;
	private ColorRegistry fColorRegistry;

	//private Map fDisabledImageDescriptors = new HashMap();

	/**
	* @param customizationInfo
	*/
	public ExtensionCustomizationLabelProvider(ICustomizationInfo customizationInfo, FormToolkit toolkit) {
		this.fToolkit = toolkit;
		this.fCustomizationInfo = customizationInfo;
		fColorRegistry = new ColorRegistry();
		fColorRegistry.put(DISABLED, FormColors.blend(fToolkit.getColors().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND), fToolkit.getColors().getSystemColor(SWT.COLOR_WHITE), 50));
		fColorRegistry.put(TRANSFORMED, FormColors.blend(fToolkit.getColors().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND), fToolkit.getColors().getSystemColor(SWT.COLOR_GREEN), 50));
		fLabelProviders = new HashMap();
		//TODO add label providers by type
//		addLabelProvider(new String[] {"org.eclipse.ui.category"}, new LabelProvider() {
//
//			public Image getImage(Object element) {
//				return null;
//			}
//
//			public String getText(Object element) {
//				//TODO this is only an example
//				IPluginElement elem = (IPluginElement) element;
//				return elem.getName();
//			}
//
//		});
		addLabelProvider(new String[] {NAMED_PROVIDER_ID}, new LabelProvider() {

			public Image getImage(Object element) {
				return null;
			}

			public String getText(Object object) {
				//TODO add transformation support
				IPluginElement element = (IPluginElement) object;
				IPluginAttribute labelAtt = element.getAttribute(IPluginObject.P_NAME);
				if (labelAtt == null) {
					return null;
				}
				String baseName = element.getName();
				String fullName = null;
				if (labelAtt.getValue() != null)
					fullName = ExtensionsSection.stripShortcuts(labelAtt.getValue());
				fullName = element.getResourceString(fullName);
				if (BidiUtil.isBidiPlatform())
					return fullName + " \u200f(\u200e" + baseName + ")"; //$NON-NLS-1$ //$NON-NLS-2$
				return fullName + " (" + baseName + ')'; //$NON-NLS-1$
			}

		});
	}

	public String getText(Object obj) {
		if (obj instanceof IPluginElement) {
			IPluginElement elem = (IPluginElement) obj;
			String name = elem.getPluginBase().getId() + "." + elem.getName();
			ILabelProvider provider = (ILabelProvider) fLabelProviders.get(name);
			if (provider != null) {
				return provider.getText(obj);
			}
			if (elem.getAttribute(IPluginObject.P_NAME) != null) {
				provider = (ILabelProvider) fLabelProviders.get(NAMED_PROVIDER_ID);
				provider.getText(obj);
			}
		}
		return super.getText(obj);
	}

	/*
	* (non-Javadoc)
	* 
	* @see org.eclipse.jface.viewers.BaseLabelProvider#dispose()
	*/
	public void dispose() {
		if (fDisabledColor != null)
			fDisabledColor.dispose();
		fResourceManager.dispose();
	}

	/*
	* (non-Javadoc)
	* 
	* @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
	*/
	public Color getBackground(Object element) {
		return null;
	}

	/*
	* (non-Javadoc)
	* 
	* @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
	*/
	public Color getForeground(Object element) {

		ITransformationsManager manager = fCustomizationInfo.getTransformsManager();
		//TODO change if there are other than IPluginObject;
		if (element instanceof IPluginObject) {
			if (manager.isAffected((IPluginObject) element))
				return getDisabledColor();
			if (manager.hasTransforms((IPluginObject) element))
				return getHasTransformColor();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
	 */
	public Font getFont(Object element) {
		ITransformationsManager manager = fCustomizationInfo.getTransformsManager();
		//TODO change if there are other than IPluginObject;
		if (element instanceof IPluginObject) {
			if (manager.isTransformed((IPluginObject) element)) {
				return getDisabledFont();
			}
		}
		return null;
	}

	/*
	* (non-Javadoc)
	* 
	* @see org.eclipse.pde.internal.ui.editor.plugin.ExtensionLabelProvider#getImage(java.lang.Object)
	*/
	public Image getImage(Object obj) {
		//IProductTransform[] transforms = fCustomizationInfo.getTransforms();
		//
		//for (int i = 0; i < transforms.length; i++) {
		//	IProductTransform productTransform = transforms[i];
		//	if (productTransform.getTransformedObject().equals(obj)) {
		//
		//		return getDisabledImage(obj);
		//	}
		//}
		return super.getImage(obj);
	}

	/**
	* @param image
	* @return
	*/
	//private Image getDisabledImage(Object object) {
	//ImageDescriptor descriptor = (ImageDescriptor) fDisabledImageDescriptors
	//		.get(object);
	//if (descriptor == null) {
	//	final Image superImage = super.getImage(object);
	//	descriptor = new CompositeImageDescriptor() {
	//
	//		protected void drawCompositeImage(int width, int height) {
	//			Rectangle bounds = superImage.getBounds();
	////			ImageData data = (ImageData) superImage.getImageData().clone();
	//			ImageData data = superImage.getImageData();
	//			int[] pixels = new int[bounds.width];
	//			for (int i = 0; i < bounds.height; i++) {
	//				data.getPixels(0, i, bounds.width, pixels, 0);
	//				for (int j = 0; j < pixels.length; j++) {
	//					System.out.print(j + "," + i + " : ");
	//					System.out.print(pixels[j] + " = ");
	//					float r = (pixels[j] & 0xFF) / 255f, g = ((pixels[j] >> 8) & 0xFF) / 255f, b = ((pixels[j] >> 16) & 0xFF) / 255f;
	//					System.out.print(r + "," + g + "," + b + " -> ");
	//					r *= 1.2f;
	//					g *= 1.2f;
	//					b *= 1.2f; 
	//					int rInt = ((int)(Math.min(r * 255, 255)));
	//					int gInt = ((int)(Math.min(r * 255, 255))) << 8;
	//					int bInt = ((int)(Math.min(r * 255, 255))) << 16;
	//					pixels[j] = rInt + gInt + bInt;
	//					System.out.println(r + "," + g + "," + b);
	////					pixels[j] = ((int) (((pixels[j] & 0xFF) / 255) * 1.5) * 255)
	////							| ((int) (((((pixels[j] >> 8) & 0xFF) / 255) * 1.5) * 255) << 8)
	////							| ((int) (((((pixels[j] >> 16) & 0xFF) / 255) * 1.5) * 255) << 16);
	//					// pixels[j] = ((int)((pixels[j] & 0xFF)/255) *
	//					// 1.5) * 255)) | ((int)(([j] & 0xFF)/255)
	//					// * 1.5) * 255)) | ((int)((pixels[j] &
	//					// 0xFF)/255) * 1.5) * 255));
	//					
	//				}
	//				data.setPixels(0, i, bounds.width, pixels, 0);
	//			}
	//			//drawImage(superImage.getImageData(), 0, 0);
	//			drawImage(data, 0, 0);
	//		}
	//
	//		protected Point getSize() {
	//			return new Point(superImage.getBounds().width,
	//					superImage.getBounds().height);
	//		}
	//	};
	//}
	//return fResourceManager.createImage(descriptor);
	//}
	/**
	* @return TODO add comment
	*/
	private Color getDisabledColor() {
		return fColorRegistry.get(DISABLED);
	}

	private Color getHasTransformColor() {
		return fColorRegistry.get(TRANSFORMED);
	}

	/**
	 * @return font data
	 */
	private Font getDisabledFont() {
		if (fDisabledFont == null) {
			fDisabledFont = JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT);
		}
		return fDisabledFont;
	}

	/**
	 * AddLabelProvider for custom extension, successful only when all can be added
	 * @param provider label provider for extension
	 * @param ids - identifiers for which can provide label
	 * @return true if successful added for all
	 */
	public boolean addLabelProvider(String[] ids, ILabelProvider provider) {
		if (ids == null)
			return true;
		for (int i = 0; i < ids.length; ++i) {
			if (fLabelProviders.containsKey(ids[i]))
				return false;
		}
		for (int i = 0; i < ids.length; ++i) {
			fLabelProviders.put(ids[i], provider);
		}
		return true;
	}
}
